'use strict';
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
const d = require('../doc/anchors.cjs'),
  a = require('../nodes/identity.cjs'),
  c = require('./stringifyComment.cjs'),
  g = require('./stringifyString.cjs');
function S(t, e) {
  const s = Object.assign(
    {
      blockQuote: !0,
      commentString: c.stringifyComment,
      defaultKeyType: null,
      defaultStringType: 'PLAIN',
      directives: null,
      doubleQuotedAsJSON: !1,
      doubleQuotedMinMultiLineLength: 40,
      falseStr: 'false',
      flowCollectionPadding: !0,
      indentSeq: !0,
      lineWidth: 80,
      minContentWidth: 20,
      nullStr: 'null',
      simpleKeys: !1,
      singleQuote: null,
      trueStr: 'true',
      verifyAliasOrder: !0,
    },
    t.schema.toStringOptions,
    e,
  );
  let r;
  switch (s.collectionStyle) {
    case 'block':
      r = !1;
      break;
    case 'flow':
      r = !0;
      break;
    default:
      r = null;
  }
  return {
    anchors: new Set(),
    doc: t,
    flowCollectionPadding: s.flowCollectionPadding ? ' ' : '',
    indent: '',
    indentStep: typeof s.indent == 'number' ? ' '.repeat(s.indent) : '  ',
    inFlow: r,
    options: s,
  };
}
function y(t, e) {
  var o;
  if (e.tag) {
    const n = t.filter(i => i.tag === e.tag);
    if (n.length > 0) return n.find(i => i.format === e.format) ?? n[0];
  }
  let s, r;
  if (a.isScalar(e)) {
    r = e.value;
    const n = t.filter(i => {
      var l;
      return (l = i.identify) == null ? void 0 : l.call(i, r);
    });
    s = n.find(i => i.format === e.format) ?? n.find(i => !i.format);
  } else (r = e), (s = t.find(n => n.nodeClass && r instanceof n.nodeClass));
  if (!s) {
    const n =
      ((o = r == null ? void 0 : r.constructor) == null ? void 0 : o.name) ??
      typeof r;
    throw new Error(`Tag not resolved for ${n} value`);
  }
  return s;
}
function h(t, e, { anchors: s, doc: r }) {
  if (!r.directives) return '';
  const o = [],
    n = (a.isScalar(t) || a.isCollection(t)) && t.anchor;
  n && d.anchorIsValid(n) && (s.add(n), o.push(`&${n}`));
  const i = t.tag ? t.tag : e.default ? null : e.tag;
  return i && o.push(r.directives.tagString(i)), o.join(' ');
}
function v(t, e, s, r) {
  var f;
  if (a.isPair(t)) return t.toString(e, s, r);
  if (a.isAlias(t)) {
    if (e.doc.directives) return t.toString(e);
    if ((f = e.resolvedAliases) != null && f.has(t))
      throw new TypeError(
        'Cannot stringify circular structure without alias nodes',
      );
    e.resolvedAliases
      ? e.resolvedAliases.add(t)
      : (e.resolvedAliases = new Set([t])),
      (t = t.resolve(e.doc));
  }
  let o;
  const n = a.isNode(t) ? t : e.doc.createNode(t, { onTagObj: u => (o = u) });
  o || (o = y(e.doc.schema.tags, n));
  const i = h(n, o, e);
  i.length > 0 && (e.indentAtStart = (e.indentAtStart ?? 0) + i.length + 1);
  const l =
    typeof o.stringify == 'function'
      ? o.stringify(n, e, s, r)
      : a.isScalar(n)
        ? g.stringifyString(n, e, s, r)
        : n.toString(e, s, r);
  return i
    ? a.isScalar(n) || l[0] === '{' || l[0] === '['
      ? `${i} ${l}`
      : `${i}
${e.indent}${l}`
    : l;
}
exports.createStringifyContext = S;
exports.stringify = v;
